#include "includes.h"

#include "communication.h"					//ucos 使用	
#include "usart.h"	
#include "key.h"	
#include "command_parse.h"					//ucos 使用	
#include <stdlib.h>
#include <math.h>

extern OS_EVENT *MutexIIC;
CAL_Info_Type CAL_State;

//unsigned  char  Cal_state;         //校表状态
//unsigned  char  Cal_wavelength;    //校表波长
//unsigned  char  Cal_level;         //校表档位
//unsigned  char  point;             //校表档位
//float           Cal_mw[8];         //标准表功率（mw）
//float           Cal_vol[8];        //校表测量电压  

void Init_CAL_State(void)
{
	unsigned char i ;
	
	CAL_State.Cal_level =0;
	CAL_State.Cal_state =Close;
	CAL_State.Cal_wavelength =0;
	CAL_State.point =0;
	
	for(i=0;i<8;i++)
	{
		CAL_State.Cal_mw[i] =0;
		CAL_State.Cal_vol[i] =0;
	}
}

void Ost_Sup()
{
    OSTaskSuspend(TIME_PRIO);
    OSTaskSuspend(AUTOMATISM_PRIO);
    OSTaskSuspend(HOST_PRIO);
    OSTaskSuspend(POWEROFF_PRIO);
    OSTaskSuspend(SOURCE_PRIO);
}

void Ost_Rem()
{
		FillDisplay1(0, 240, 320, 240, 0xffff);
		FillDisplay1(100, 130, 80, 24, LCD_WHITE);
		HostImage();
		OSTaskResume(SOURCE_PRIO);
		OSTaskResume(HOST_PRIO);
		OSTaskResume(AUTOMATISM_PRIO);
		OSTaskResume(TIME_PRIO);
		OSTaskResume(POWEROFF_PRIO);
}

void Ost_Sup1()
{
   
//    OSTaskSuspend(AUTOMATISM_PRIO);
//    OSTaskSuspend(HOST_PRIO);
//    OSTaskSuspend(POWEROFF_PRIO);
//    OSTaskSuspend(SOURCE_PRIO);
}

void Ost_Rem1()
{
//		OSTaskResume(SOURCE_PRIO);
//		OSTaskResume(HOST_PRIO);
//		OSTaskResume(AUTOMATISM_PRIO);
//		OSTaskResume(POWEROFF_PRIO);
}


void agreement_parse(void)
{
	if((Uart_Recv.rx_buf[0] == 0xA5) && (Uart_Recv.rx_buf[1] == 0x5A ))
	{
	  //丢到packer解析，成功了调用callback
    //upacker_unpack(&msg_packer, (uint8_t *)Uart1_Recv.rx_buf, Uart1_Recv.rxlen);
    // Clear_Uart(&Uart1_Recv);
	}
	else
	{
	   // TODO: 开始解析字符串
     communicate_parse((const char *)Uart_Recv.rx_buf);
	}
	UartRecvStructInit(&Uart_Recv);
	state = 0;  //USB线程开启标志
}

/**
 * @function: wavelength_trma
 * @brief: 字符串转枚举
 * @param {*}
 * @return {*}
 * @author: clc
 */
unsigned char wavelength_trma(Cmd_Info_Type x)
{	
	if(strcmp("CW1310\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =CW1310;
	}
	else if(strcmp("CW1490\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =CW1490;
	}
	else if(strcmp("CW1550\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =CW1550;
	}
	else if(strcmp("BU1310\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =BU1310;
	}
#if FHP3P05
	else if(strcmp("BU1270\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =BU1270;
	}
	else if(strcmp("CW1270\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =CW1270;
	}
	else if(strcmp("CW1577\n", (const char *)x.data[0]) == 0)
	{
		CAL_State.Cal_wavelength =CW1577;
	}
#endif
	else
	{
		CAL_State.Cal_wavelength  =0;
	}
	return CAL_State.Cal_wavelength;
}


/****************************************************************************************
** Name	 	: void key1_moni(void)
** Function : the key1_moni is pressed.
** Input	: NULL
** Output	: NULL
*****************************************************************************************/

void key1_moni(Cmd_Info_Type x)
{
    if(g_c_powerFlag & 0x02)	//若为充电状态退出时，只置位标志位
    {
      if(x.data[0][0]=='l')
			{
				g_c_LcdUseNum = LCD_USE_HOST;
				CheckPower(1);
				first_display();//显示最开始界面函数
				g_c_powerFlag = 0x01;
				OSTaskResume(HOST_PRIO);			
				OSTaskResume(TIME_PRIO);
				OSTaskResume(AUTOMATISM_PRIO);
				OSTaskResume(POWEROFF_PRIO);    
			}				
    }
    else
    {

        if(x.data[0][0]=='l')
        {
            pt = OFF_Key;
        }
        else if(x.data[0][0]=='s')								      /*short press,auto-off or not*/
        {
            if(g_c_LcdUseNum == LCD_USE_HOST)		 //如果不在主界面，则短按无效
            {
                pt = Power_Key;
            }
        }
    }
}

/**
 * @function: idn
 * @brief: 获取设备类型
 * @param {*}
 * @return {*}
 * @author: lzc
 */
static void my_idn(void)
{
	;
}

/**
 * @function: *ver
 * @brief: 获取版本
 * @param {*}
 * @return {*}
 * @author: lzc
 */
static void my_ver(void)
{
//    // DONE: 完善时间戳
//    char year[10], month[10], day[10] = {0};
//    MSG_OUTPUT("SoftVer: ");
//    MSG_OUTPUT(" ");
//    // 合成数据
//    Get_YY_MM_DD_From_Str();

//    sprintf(year, "%d", SoftVer_date_buf[2]);
//    sprintf(month, "%d", SoftVer_date_buf[3]);
//    sprintf(day, "%d", SoftVer_date_buf[4]);
//    // 输出
//    MSG_OUTPUT("20");
//    MSG_OUTPUT(year);
//    MSG_OUTPUT("-");
//    MSG_OUTPUT(month);
//    MSG_OUTPUT("-");
//    MSG_OUTPUT(day);
//    MSG_OUTPUT("|");
//    MSG_OUTPUT(COMPILE_TIME);
//    MSG_OUTPUT("\n");

//    MSG_OUTPUT("HardVer: ");
//    MSG_OUTPUT(" ");
//    MSG_OUTPUT("\n");
	
	printf("SoftVer:V7.01\n");
	printf("HardVer:V7.01\n");
	
}

/**
 * @function: rst
 * @brief: 复位
 * @param {*}
 * @return {*}
 * @author: lzc
 */
static void my_rst(void)
{  
	UartRecvStructInit(&Uart_Recv);
	state = 0;  //USB线程开启标
	
	printf("ok\n");
	__set_FAULTMASK(1);
	NVIC_SystemReset();
	
}

unsigned char msg_key_set_fun(Cmd_Info_Type x)
{
	if((g_c_taskFlag & TASK_CALIBRATE) == 0)//非校准状态
	{
			if(x.data[0][0] =='s')
			{
				if(x.data[0][1]=='1')
				{
					if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
					{
						g_c_powerFlag |= 0x80;
						OSTimeDlyResume(POWEROFF_PRIO);
					}					
				  key1_moni(x); /*long press,turn off the pon*/
				}
			
			else if(x.data[0][1]=='2')
			{
				if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
				{
					g_c_powerFlag |= 0x80;
					OSTimeDlyResume(POWEROFF_PRIO);
				}								
				key2();
			}
			
			else if(x.data[0][1]=='3')
			{
				if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
				{
					g_c_powerFlag |= 0x80;
					OSTimeDlyResume(POWEROFF_PRIO);
				}								
				key3();
			}
			
			else if(x.data[0][1]=='4')
			{
				if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
				{
					g_c_powerFlag |= 0x80;
					OSTimeDlyResume(POWEROFF_PRIO);
				}								
				key4();
			}
			
			else if(x.data[0][1]=='5')
			{
				if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
				{
					g_c_powerFlag |= 0x80;
					OSTimeDlyResume(POWEROFF_PRIO);
				}								
				key5();
			}
			
			else if(x.data[0][1]=='6')
			{
				if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
				{
					g_c_powerFlag |= 0x80;
					OSTimeDlyResume(POWEROFF_PRIO);
				}								
				key6();
			}
			else
			{
				printf("err_par\n");
			}
		}
		else
		{
			 if(x.data[0][0] =='l')
			 {
				if(x.data[0][1]=='1')
				{
					if(g_c_powerFlag & 0x01) ////新按键按下，重新计时10分钟
					{
						g_c_powerFlag |= 0x80;
						OSTimeDlyResume(POWEROFF_PRIO);
					}					
					key1_moni(x); /*long press,turn off the pon*/
				}
				
				else if(x.data[0][1]=='2')
				{
				
				}
				
				else if(x.data[0][1]=='3')
				{
				}
				
				else if(x.data[0][1]=='4')
				{
				
				}
				
				else if(x.data[0][1]=='5')
				{
				
				}
				
				else if(x.data[0][1]=='6')
				{
					
				}
			}
		}
	}
	else
	{
		if(x.data[0][0] =='s')
		{
			if(x.data[0][1]=='1')
			{
				g_c_s1 = 1;
			}
			else if(x.data[0][1]=='2')
			{
				g_c_s1 = 2;
			}
			else if(x.data[0][1]=='3')
			{
				g_c_s1 = 3;
			}
			else if(x.data[0][1]=='4')
			{
				g_c_s1 = 4;
			}
			else if(x.data[0][1]=='5')
			{
				g_c_s1 = 5;
			}
			else if(x.data[0][1]=='6')
			{
				g_c_s1 = 6;
			}
			else
			{
				printf("err_par\n");
			}
		}
	}
	printf("ok\n");
	return 0;
}



/********************************************************************************************/
/********************************************************************************************/
/*************************************字符串方法1通讯协议*************************************/
/****************************************Ver 1.0.0*******************************************/
/********************************************************************************************/
/********************************************************************************************/
///// 使用示例：
/////  1.在命令列表中增加指令 共三处
/////  2.在空白函数中完善代码
/**
 * @function:
 * @brief:
 * @param {*}
 * @return {*}
 * @author: lzc
 */
unsigned  char msg_idn_fun(Cmd_Info_Type x)     
{
	u8 pBuffer[20];
	u8 err;
	
	memset(pBuffer,0,20);
	OSMutexPend(MutexIIC,200,&err);
		AT24CXX_Read(E2PROM_ADRESS, DATA_PRODUCT_NAME_ADDRESS, pBuffer, 20);
	OSMutexPost(MutexIIC);
	printf("Product Name:%s\n",pBuffer);
	memset(pBuffer,0,20);
	
	OSMutexPend(MutexIIC,200,&err);
		AT24CXX_Read(E2PROM_ADRESS, DATA_SN_ADDRESS, pBuffer, 20);
	OSMutexPost(MutexIIC);
	printf("SN:%s\n",pBuffer);

  return 0;
	
		
//	printf("SoftVer:V7.01\n");
//	printf("HardVer:V7.01\n");
}

/**
 * @function:
 * @brief:
 * @param {*}
 * @return {*}
 * @author: lzc
 */
uint8_t msg_ver_fun(Cmd_Info_Type x)
{
    my_ver();
    return 0;
}

/**
 * @function: msg_rst_fun
 * @brief: 复位
 * @param {*}
 * @return {*}
 * @author: lzc
 */
uint8_t msg_rst_fun(Cmd_Info_Type x)
{
    my_rst();
    return 0;
}

/**
 * @function: msg_get_power_fun
 * @brief: 获取功率
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_get_power_fun(Cmd_Info_Type x)
{
	  fp32  Temp = {0};
		char  POW[10]={0};

		if(strcmp("CW1550\n", (const char *)x.data[0]) == 0)
    {
			OSTimeDly(OS_TICKS_PER_SEC * 3);
			//  AutoCheck_1550();						  //1550自动检测
//			if(g_c_1550_flag == OVERFLOW_HIGH)
//			{
//					Temp = 21.0;
//			}
//			else if(g_c_1550_flag == OVERFLOW_LOW)
//			{
//					Temp = -51.0;
//			}
//			else
//			{
//					Temp = g_c_1550_pdbm;
//			}
			   Temp = g_c_1550_pdbm;
			
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
    }
		else if(strcmp("CW1490\n", (const char *)x.data[0]) == 0)
    {     
			OSTimeDly(OS_TICKS_PER_SEC * 3);
			if(g_c_1490_flag == OVERFLOW_HIGH)
			{
				Temp= 21.0;
			}
			else if(g_c_1490_flag == OVERFLOW_LOW)
			{
        Temp = -51.0;
			}
			else
			{
        Temp = g_c_1490_pdbm;
			}
			 
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
    }
		else if(strcmp("CW1310\n", (const char *)x.data[0]) == 0)
    {
			OSTimeDly(OS_TICKS_PER_SEC * 4);
			if((g_c_1310_c_flag == OVERFLOW_HIGH) || (g_c_1310_c_pdbm > 10.0))
			{
        Temp = 21.0;
			}
			else if((g_c_1310_c_flag == OVERFLOW_LOW) || (g_c_1310_c_pdbm < -40.0))
			{
        Temp = -51.0;
			}
			else
			{
        Temp = g_c_1310_c_pdbm; 
			}
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
		}
		else if(strcmp("BU1310\n", (const char *)x.data[0]) == 0)
    {
			OSTimeDly(OS_TICKS_PER_SEC * 4);
			if((g_c_1310_p_flag == OVERFLOW_HIGH) || (g_c_1310_p_pdbm > 10.0))
			{
        Temp = 21.0;
			}
			else if((g_c_1310_p_flag == OVERFLOW_LOW) || (g_c_1310_p_pdbm < -40.0))
			{
        Temp = -51.0;
			}
			else
			{
        Temp = g_c_1310_p_pdbm;
			}
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
    }
#if FHP3P05
		else if(strcmp("BU1270\n", (const char *)x.data[0]) == 0)
    {
			OSTimeDly(OS_TICKS_PER_SEC * 4);
			if((g_c_1270_p_flag == OVERFLOW_HIGH) || (g_c_1270_p_pdbm > 10.0))
			{
        Temp = 21.0;
			}
			else if((g_c_1270_p_flag == OVERFLOW_LOW) || (g_c_1270_p_pdbm < -40.0))
			{
        Temp = -51.0;
			}
			else
			{
        Temp = g_c_1270_p_pdbm;
			}
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
    }
		
		else if(strcmp("CW1270\n", (const char *)x.data[0]) == 0)
    {
			OSTimeDly(OS_TICKS_PER_SEC * 4);
			if((g_c_1270_c_flag == OVERFLOW_HIGH) || (g_c_1270_c_pdbm > 10.0))
			{
        Temp = 21.0;
			}
			else if((g_c_1270_c_flag == OVERFLOW_LOW) || (g_c_1270_c_pdbm < -40.0))
			{
        Temp = -51.0;
			}
			else
			{
        Temp = g_c_1270_c_pdbm; 
			}
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
		}
		
		else if(strcmp("CW1577\n", (const char *)x.data[0]) == 0)
    {
			OSTimeDly(OS_TICKS_PER_SEC * 3);
			//  AutoCheck_1550();						  //1550自动检测
//			if(g_c_1577_flag == OVERFLOW_HIGH)
//			{
//					Temp = 21.0;
//			}
//			else if(g_c_1577_flag == OVERFLOW_LOW)
//			{
//					Temp = -51.0;
//			}
//			else
//			{
//					Temp = g_c_1577_pdbm;
//			}
			   Temp = g_c_1577_pdbm;
			
			sprintf(POW,"%.2f\n",Temp);
			printf("%s\n",POW);
    }
#endif		
		else
		{
			printf("err_par\n");
		}
    return 0;
}

/**
 * @function: msg_get_history_fun
 * @brief: 获取历史记录
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_get_history_fun(Cmd_Info_Type x)
{
    fp32 datanum = 0;
    char  sum[4] = {0},i;
    uint16 address = 0;
		fp32   bufRecord[9] = {0};
    uint8 flaguint = 0;
	#if FHP3P05
		uint8 flaghost = 0;
	#endif
		u8 err;
		char  danwei[6],ref1313[6],gonglv1310[6],ref1490[6],gonglv1490[6],ref1550[6],gonglv1550[6];
		
	 if(x.data[0][0] ==0x00)
	 {   
		 datanum = ReadDataNum();							 ////读取存储的历史数据的条数

     TempRecordNum = 1;
		 
		 	sprintf(sum,"%f\n",datanum);
			printf("%s\n",sum);
	 }
	 else
	 {
		 i =atoi((char*)x.data[0]);        //前两位是20
		 
	 OSMutexPend(MutexIIC,200,&err); 
		 address = ReadDataE2promTwo(DATA_ADDRESS_SP + (i - 1) * 2); ///读出数据保存的初始地址
     flaguint = ReadUint(address + 18);                 ///读出保存的单位0：dBm,1:dB

#if FHP3P05		 
		flaghost =ReadHost(address + 19);
#endif
		 
    bufRecord[2] = ReadFloatE2prom(address);	                //1310
    bufRecord[4] = ReadFloatE2prom(address + 4);				 //1490
    bufRecord[6] = ReadFloatE2prom(address + 8);				 //1550
	OSMutexPost(MutexIIC);
    if(flaguint == 1)							//如果为dB 值
    {
        bufRecord[0] = 0;					 //// bufRecord[0] 的第一个字节 存入 0x00
		 OSMutexPend(MutexIIC,200,&err); 	
        bufRecord[1] = ReadFloatE2prom2(address + 12);
        bufRecord[3] = ReadFloatE2prom2(address + 14);
        bufRecord[5] = ReadFloatE2prom2(address + 16);
		 OSMutexPost(MutexIIC);
    }
    else
    {
        bufRecord[0] = -21.25049; ///2.369428e-038;	    // bufRecord[0] 的第一个字节 存入 0x01
        bufRecord[1] = 0;
        bufRecord[3] = 0;
        bufRecord[5] = 0;
    }
	
		sprintf(danwei,"%.2f",bufRecord[0]);
		
		sprintf(ref1313,"%.2f",bufRecord[1]);
		sprintf(gonglv1310,"%.2f",bufRecord[2]);
		
		sprintf(ref1490,"%.2f",bufRecord[3]);	
		sprintf(gonglv1490,"%.2f",bufRecord[4]);
		
		sprintf(ref1550,"%.2f",bufRecord[5]);
		sprintf(gonglv1550,"%.2f",bufRecord[6]);
	
#if FHP3P05
if(flaghost ==0)
{
		if(flaguint==1)
		{
			printf("1310nm %sdb %sdbm 1 null\n 1490nm %sdb %sdbm 1 null\n 1550nm %sdb %sdbm 1 null\n",
			     gonglv1310,ref1313,gonglv1490,ref1490,gonglv1550,ref1550);           //波长，功率，参考值，单位，频率
		}
		else 
		{
			printf("1310nm %sdbm %sdbm 2 null\n 1490nm %sdbm %sdbm 2 null\n 1550nm %sdbm %sdbm 2 null\n",
					gonglv1310,ref1313,gonglv1490,ref1490,gonglv1550,ref1550);           //波长，功率，参考值，单位，频率
		}
}
else if(flaghost ==1)
{
		if(flaguint==1)
		{
			printf("1270nm %sdb %sdbm 1 null\n 1490nm %sdb %sdbm 1 null\n 1577nm %sdb %sdbm 1 null\n",
			     gonglv1310,ref1313,gonglv1490,ref1490,gonglv1550,ref1550);           //波长，功率，参考值，单位，频率
		}
		else 
		{
			printf("1270nm %sdbm %sdbm 2 null\n 1490nm %sdbm %sdbm 2 null\n 1577nm %sdbm %sdbm 2 null\n",
					gonglv1310,ref1313,gonglv1490,ref1490,gonglv1550,ref1550);           //波长，功率，参考值，单位，频率
		}
}
#else	
		if(flaguint==1)
		{
			printf("1310nm %sdb %sdbm 1 null\n 1490nm %sdb %sdbm 1 null\n 1550nm %sdb %sdbm 1 null\n",
			     gonglv1310,ref1313,gonglv1490,ref1490,gonglv1550,ref1550);           //波长，功率，参考值，单位，频率
		}
		else 
		{
			printf("1310nm %sdbm %sdbm 2 null\n 1490nm %sdbm %sdbm 2 null\n 1550nm %sdbm %sdbm 2 null\n",
					gonglv1310,ref1313,gonglv1490,ref1490,gonglv1550,ref1550);           //波长，功率，参考值，单位，频率
		}
#endif		
	 }
    return 0;
}

/**
 * @function: msg_get_history_fun
 * @brief: 获取当前波长电压
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_get_vol_fun(Cmd_Info_Type x)
{
	unsigned char  wavelength;
	unsigned char  a0a1;
	unsigned char  i;
	static	float  voltage;
  char	         buf[15] = {0};
	char           vol[10];

	i =(CAL_State.Cal_level)*2+CAL_State.point;   //液晶显示第几列、
	
	if(CAL_State.Cal_state ==Open)
	{
		wavelength =wavelength_trma(x);
		a0a1 = CAL_State.Cal_level;
		
		if(wavelength ==CAL_State.Cal_wavelength)
		{		 
			if(CAL_State.Cal_wavelength ==BU1310 ||CAL_State.Cal_wavelength ==CW1310 
							||CAL_State.Cal_wavelength ==BU1270 ||CAL_State.Cal_wavelength ==CW1270)
			{
				if((CAL_State.Cal_wavelength ==BU1310) ||(CAL_State.Cal_wavelength ==BU1270))   //突发光
				{
					ARM_LOGIC0 = 0;							/*able the pulse of 1310 */
				}
				else
				{
					ARM_LOGIC0 = 1;							/*disable the pulse of 1310 */
				}
				
				Channel(AD7799_1310);     //AD口子确认
				delay_ms(50);
				Set1310Adg704(a0a1);
				ClrVolt1310();
        delay_ms(2);
        EnVolt1310();
			}
			
			else if(CAL_State.Cal_wavelength ==CW1490 )
			{
				Channel(AD7799_1490);     //AD口子确认
				delay_ms(50);
				Set1490Adg704(a0a1);
        ReleaseVolt1490();
			
			}
			
			else if(CAL_State.Cal_wavelength ==CW1550 ||CAL_State.Cal_wavelength ==CW1577)
			{
				Channel(AD7799_1550);     //AD口子确认
				delay_ms(50);   
        Set1550Adg704(a0a1);
        ReleaseVolt1550();
			}
			else
			{	
				printf("err_par\n");
				return 0;
			}

			delay_ms(2000);
			voltage = Ad7799Result();
			
			CAL_State.Cal_vol[(CAL_State.Cal_level)*2+CAL_State.point-1] = voltage;
			
			if(SPRINT_ERROR)										 // 显示采样电压值
			{
					FloatToStr(buf, 3, voltage);
					Dis_Num(190, 180 - 20 * (i - 1), 12, 16, buf);
			}
			else
			{
					sprintf(buf, "%5.4f\n", voltage);
					Dis_Num(190, 180 - 20 * (i - 1), 12, 16, buf);
			}
			sprintf(vol,"%.4f\n",voltage);
			printf("%s\n",vol);
			return 0;
		}
		else
		{
			printf("err_par\n");
			CAL_State.Cal_state =Close;
		}
	}
	printf("err_cal\n");
	CAL_State.Cal_state =Close;
	return 0;
}

unsigned char msg_get_threshold_name_fun(Cmd_Info_Type x)
{

	uint8 num = 0;
	uint8 buf[50];
	u8 err;
	
	num =atoi((char*)x.data[0]);        //前两位是20
	num =num-1;
OSMutexPend(MutexIIC,200,&err);	
	ReadStringE2prom(buf, PWF_NAME_ADDRESS + NAME_LENGTH * (num));
OSMutexPost(MutexIIC);	
	printf("%s\n",buf);
	return 0;
}

unsigned char msg_get_threshold_1310_fun(Cmd_Info_Type x)
{
	uint8 num = 0,i =0;
	char b[6],c[6],d[6];
	fp32 bufpwf[3] ={0};
	u8 err;
	
	num =atoi((char*)x.data[0]);        //前两位是20
	num =num-1;
	
	for(i = 0; i < 3; i++)
  {
		OSMutexPend(MutexIIC,200,&err);
      bufpwf[i] = ReadFloatE2prom(PWF_ADDRESS + i * 4 + 12*0+(num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);
  }
	
	sprintf(b,"%.2f",bufpwf[0]);
	sprintf(c,"%.2f",bufpwf[1]);
	sprintf(d,"%.2f\n",bufpwf[2]);
	
	printf("%s,%s,%s\n",b,c,d);
	return 0;
}
unsigned char msg_get_threshold_1490_fun(Cmd_Info_Type x)
{
	uint8 num = 0,i =0;
	char b[6],c[6],d[6];
	fp32 bufpwf[3] ={0};
	u8 err;
	
	num =atoi((char*)x.data[0]);        //前两位是20
	num =num-1;
	
	for(i = 0; i < 3; i++)
  {
		OSMutexPend(MutexIIC,200,&err);
      bufpwf[i] = ReadFloatE2prom(PWF_ADDRESS + i * 4 + 12*1+(num) * 36); /*write 10 team of the PWF*/	
		OSMutexPost(MutexIIC);
  }
	
	sprintf(b,"%.2f",bufpwf[0]);
	sprintf(c,"%.2f",bufpwf[1]);
	sprintf(d,"%.2f\n",bufpwf[2]);
	
	printf("%s,%s,%s\n",b,c,d);
		return 0;
}
unsigned char msg_get_threshold_1550_fun(Cmd_Info_Type x)
{
	uint8 num = 0,i =0;
	char b[6],c[6],d[6];
	fp32 bufpwf[3] ={0};
	u8 err;
	
	num =atoi((char*)x.data[0]);        //前两位是20
	num =num-1;
	
	for(i = 0; i < 3; i++)
  {
		OSMutexPend(MutexIIC,200,&err);	
      bufpwf[i] = ReadFloatE2prom(PWF_ADDRESS + i * 4 + 12*2+(num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);	
  }
	
	sprintf(b,"%.2f",bufpwf[0]);
	sprintf(c,"%.2f",bufpwf[1]);
	sprintf(d,"%.2f\n",bufpwf[2]);
	
	printf("%s,%s,%s\n",b,c,d);
	return 0;
}

#if FHP3P05
unsigned char msg_get_threshold_1270_fun(Cmd_Info_Type x)
{
	uint8 num = 0,i =0;
	char b[6],c[6],d[6];
	fp32 bufpwf[3] ={0};
	u8 err;
	
	num =atoi((char*)x.data[0]);        //前两位是20
	num =num-1;

	for(i = 0; i < 3; i++)
  {
		OSMutexPend(MutexIIC,200,&err);	
      bufpwf[i] = ReadFloatE2prom(P05_PWF_ADDRESS + i * 4 + 12*0+(num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);		
  }
	
	sprintf(b,"%.2f",bufpwf[0]);
	sprintf(c,"%.2f",bufpwf[1]);
	sprintf(d,"%.2f\n",bufpwf[2]);
	
	printf("%s,%s,%s\n",b,c,d);

		return 0;
}

unsigned char msg_get_threshold_1577_fun(Cmd_Info_Type x)
{
	uint8 num = 0,i =0;
	char b[6],c[6],d[6];
	fp32 bufpwf[3] ={0};
	u8 err;
	
	num =atoi((char*)x.data[0]);        //前两位是20
	num =num-1;
	
	for(i = 0; i < 3; i++)
  {
		OSMutexPend(MutexIIC,200,&err);
      bufpwf[i] = ReadFloatE2prom(P05_PWF_ADDRESS + i * 4 + 12*2+(num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);			
  }
	
	sprintf(b,"%.2f",bufpwf[0]);
	sprintf(c,"%.2f",bufpwf[1]);
	sprintf(d,"%.2f\n",bufpwf[2]);
	
	printf("%s,%s,%s\n",b,c,d);

	return 0;
}
#endif


unsigned char msg_get_wave_offset_fun(Cmd_Info_Type x)
{
	unsigned char wavelength;

	char a[10]={0};
	fp32 bufpwf[1] = {0};
	u8 err;
	
		if(strcmp("CW1310", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1310;
	}
	else if(strcmp("CW1490", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1490;
	}
	else if(strcmp("CW1550", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1550;
	}
	else if(strcmp("BU1310", (const char *)x.data[0]) == 0)
	{
		wavelength =BU1310;
	}
#if FHP3P05
	else if(strcmp("BU1270", (const char *)x.data[0]) == 0)
	{
		wavelength =BU1270;
	}
	else if(strcmp("CW1270", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1270;
	}
	else if(strcmp("CW1577", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1577;
	}
#endif
	else
	{
		wavelength  =0;
	}
	
OSMutexPend(MutexIIC,200,&err);
   if(wavelength==BU1310)
	 {
       bufpwf[0] = ReadFloatE2prom(WAVE_OFFSET_ADDRESS ); ////读取修正值
	 }
	 else if(wavelength==CW1490)
	 {
        bufpwf[0] = ReadFloatE2prom(WAVE_OFFSET_ADDRESS +  4); ////读取修正值
	 }
	 else if(wavelength==CW1550)
	 {
			 bufpwf[0] = ReadFloatE2prom(WAVE_OFFSET_ADDRESS +  8); ////读取修正值
	 }
	 else if(wavelength==CW1310)
	 {
      bufpwf[0] = ReadFloatE2prom(CAL_1310_C_ADDRESS); ////读取1310连续的修正值
	 }
#if FHP3P05
	 else if(wavelength==BU1270)
	 {
       bufpwf[0] = ReadFloatE2prom(P05_WAVE_OFFSET_ADDRESS ); ////读取修正值
	 }
	 else if(wavelength==CW1270)
	 {
      bufpwf[0] = ReadFloatE2prom(P05_CAL_1270_C_ADDRESS); ////读取1310连续的修正值
	 }
	 else if(wavelength==CW1577)
	 {
			 bufpwf[0] = ReadFloatE2prom(P05_WAVE_OFFSET_ADDRESS +  8); ////读取修正值
	 }
#endif	 
OSMutexPost(MutexIIC);	
	 
	 sprintf(a,"%.4f\n",bufpwf[0]);
	 printf("%s\n",a);
	return 0;
}

//////////////////////////////////////////设置命令/////////////////////////////////////////////


/**
 * @function: msg_set_power_fun
 * @brief: 设置 整偏
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_set_power_fun(Cmd_Info_Type x)
{
  float  Temp[1] = {0};
	float pwm_now_1490 =0;
	float pwm_now_1550 =0;
	u8 err;
		
	Temp[0] = (float)atof((char*)x.data[0]);   //转浮点数
	Temp[0] =(float)pow(10,(float)Temp[0]/10);  //dbm转mw
	
	if(CAL_State.Cal_wavelength ==CW1550)
  {
		g_c_1490_voltage = Voltage1490AotumatismTest();//测量1490nm 光强并计算光功率
		g_c_1550_voltage = Voltage1550AotumatismTest();//测量1550nm 光强并计算光功率
		pwm_now_1490 = (g_c_1490_voltage - g_c_1490_rd[g_c_1490_a0a1]) / g_c_1490_rn[g_c_1490_a0a1];
    pwm_now_1550 = (g_c_1550_voltage - g_c_1550_rd[g_c_1550_a0a1]) / g_c_1550_rn[g_c_1550_a0a1];
		g_c_1550_pmw = (Iso1490_1550 * Iso1550_1490 * pwm_now_1550 - Iso1550_1490 * pwm_now_1490) / ( Iso1490_1550 * Iso1550_1490 - 1);
			
		gc_1550_coef = (float)Temp[0] /g_c_1550_pmw;
	OSMutexPend(MutexIIC,200,&err);	
    WriteFloatE2prom(gc_1550_coef, GC_1550_COEF_ADDRESS);//save		
	OSMutexPost(MutexIIC);	
			delay_ms(10);
  }
	else if(CAL_State.Cal_wavelength ==CW1490)
  {
		g_c_1490_voltage = Voltage1490AotumatismTest();//测量1490nm 光强并计算光功率
		g_c_1550_voltage = Voltage1550AotumatismTest();//测量1550nm 光强并计算光功率
		pwm_now_1490 = (g_c_1490_voltage - g_c_1490_rd[g_c_1490_a0a1]) / g_c_1490_rn[g_c_1490_a0a1];
		pwm_now_1550 = (g_c_1550_voltage - g_c_1550_rd[g_c_1550_a0a1]) / g_c_1550_rn[g_c_1550_a0a1];
		g_c_1490_pmw = (Iso1490_1550 * Iso1550_1490 * pwm_now_1490 - Iso1490_1550 * pwm_now_1550)/(Iso1490_1550 * Iso1550_1490 - 1);

		gc_1490_coef = (float)Temp[0] /g_c_1490_pmw;
	OSMutexPend(MutexIIC,200,&err);		
    WriteFloatE2prom(gc_1490_coef, GC_1490_COEF_ADDRESS);//save
	OSMutexPost(MutexIIC);	
			delay_ms(10);

  }
	else if(CAL_State.Cal_wavelength ==CW1310)
  {
		g_c_1310_c_voltage = Voltage1310CwAotumatismTest();//测量1310nm 光强并计算光功率
		g_c_1310_c_pmw 	 = (g_c_1310_c_voltage - g_c_1310_c_rd[g_c_1310_c_a0a1]) / g_c_1310_c_rn[g_c_1310_c_a0a1];
		gc_1310_cw_coef = (float)Temp[0]/g_c_1310_c_pmw;
	OSMutexPend(MutexIIC,200,&err);		
    WriteFloatE2prom(gc_1310_cw_coef, GC_1310_CW_COEF_ADDRESS);//save
	OSMutexPost(MutexIIC);	
		delay_ms(10);
	}
	else if(CAL_State.Cal_wavelength ==BU1310)
  {
			
    
  }
	
#if FHP3P05
	else if(CAL_State.Cal_wavelength ==CW1270)
  {
		g_c_1310_c_voltage = Voltage1310CwAotumatismTest();//测量1310nm 光强并计算光功率
		g_c_1310_c_pmw 	 = (g_c_1310_c_voltage - g_c_1310_c_rd[g_c_1310_c_a0a1]) / g_c_1310_c_rn[g_c_1310_c_a0a1];
		gc_1270_c_coef = (float)Temp[0]/g_c_1310_c_pmw;
	OSMutexPend(MutexIIC,200,&err);		
    WriteFloatE2prom(gc_1270_c_coef, P05_GC_1270_C_COEF_ADDRESS);//save
	OSMutexPost(MutexIIC);	
		delay_ms(10);
	}
	else if(CAL_State.Cal_wavelength ==CW1577)
  {
		g_c_1490_voltage = Voltage1490AotumatismTest();//测量1490nm 光强并计算光功率
		g_c_1550_voltage = Voltage1550AotumatismTest();//测量1550nm 光强并计算光功率
		pwm_now_1490 = (g_c_1490_voltage - g_c_1490_rd[g_c_1490_a0a1]) / g_c_1490_rn[g_c_1490_a0a1];
    pwm_now_1550 = (g_c_1550_voltage - g_c_1550_rd[g_c_1550_a0a1]) / g_c_1550_rn[g_c_1550_a0a1];
		g_c_1550_pmw = (Iso1490_1550 * Iso1550_1490 * pwm_now_1550 - Iso1550_1490 * pwm_now_1490) / ( Iso1490_1550 * Iso1550_1490 - 1);
			
		gc_1577_coef = (float)Temp[0] /g_c_1550_pmw;
	OSMutexPend(MutexIIC,200,&err);		
    WriteFloatE2prom(gc_1577_coef, P05_GC_1577_COEF_ADDRESS);//save		
	OSMutexPost(MutexIIC);	
		delay_ms(10);
  }
#endif
	else
	{
		printf("err_par\n");
	}
	printf("ok\n");
    return 0;
}


/**
 * @function: msg_set_linear_reset_fun
 * @brief: 线性清除
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_set_linear_reset_fun(Cmd_Info_Type x)
{
	 u8	i = 0;
	
	for ( i = 0; i < 4; i++ )
	{
		g_c_1550_rn[i] =g_c_1550_rn_d[i];	/* slope of the beeline of 1550 */
	}

	for ( i = 0; i < 4; i++ )
	{
		g_c_1550_rd[i] =g_c_1550_rd_d[i];	/* the intercept of the beeline of 1550*/
	}
	
	for ( i = 0; i < 4; i++ )
	{
		g_c_1490_rn[i] =g_c_1490_rn_d[i];	/* slope of the beeline of 1550 */
	}

	for ( i = 0; i < 4; i++ )
	{
		g_c_1490_rd[i] =g_c_1490_rd_d[i];	/* the intercept of the beeline of 1550*/
	}
	
	for ( i = 0; i < 4; i++ )
	{
		g_c_1310_p_rn[i] =g_c_1310_p_rn_d[i];	/* slope of the beeline of 1550 */
	}

	for ( i = 0; i < 4; i++ )
	{
		g_c_1310_p_rd[i] =g_c_1310_p_rd_d[i];	/* the intercept of the beeline of 1550*/
	}
	
	for ( i = 0; i < 4; i++ )
	{
		g_c_1310_c_rn[i] =g_c_1310_c_rn_d[i];	/* slope of the beeline of 1550 */
	}

	for ( i = 0; i < 4; i++ )
	{
		g_c_1310_c_rd[i] =g_c_1310_c_rd_d[i];	/* the intercept of the beeline of 1550*/
	}
		printf("ok\n");
    return 0;
}


/**
 * @function: msg_set_coeff_reset_fun
 * @brief: 整偏清除
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_set_coeff_reset_fun(Cmd_Info_Type x)
{
	u8 err;
	gc_1310_cw_coef =1;
	gc_1490_coef  =1;
	gc_1550_coef  =1;
	
#if FHP3P05
	gc_1270_c_coef =1;
	gc_1577_coef  =1;
	
OSMutexPend(MutexIIC,200,&err);	
	WriteFloatE2prom(1,P05_GC_1270_C_COEF_ADDRESS);
OSMutexPost(MutexIIC);	
	delay_ms(10);
OSMutexPend(MutexIIC,200,&err);	
	WriteFloatE2prom(1,P05_GC_1577_COEF_ADDRESS);
OSMutexPost(MutexIIC);		
	delay_ms(10);
#endif
OSMutexPend(MutexIIC,200,&err);	
	WriteFloatE2prom(1,GC_1310_CW_COEF_ADDRESS);
OSMutexPost(MutexIIC);		
	delay_ms(10);
OSMutexPend(MutexIIC,200,&err);	
	WriteFloatE2prom(1,GC_1490_COEF_ADDRESS);
OSMutexPost(MutexIIC);		
	delay_ms(10);
OSMutexPend(MutexIIC,200,&err);	
	WriteFloatE2prom(1,GC_1550_COEF_ADDRESS);
OSMutexPost(MutexIIC);
	delay_ms(10);
	printf("ok\n");
  return 0;
}

/**
 * @function: msg_set_switch_fun
 * @brief: 设置当前时间
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_set_time_fun(Cmd_Info_Type x)
{
	static	unsigned char  Tmep[2];
/*	
	Tmep[0] =x.data[0][2];
	Tmep[1] =x.data[0][3];
	year =atoi((char*)Tmep);        //前两位是20.
	
	Tmep[0] =x.data[0][5];
	Tmep[1] =x.data[0][6];
	
	month =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][8];
	Tmep[1] =x.data[0][9];
	
	day =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][11];
	Tmep[1] =x.data[0][12];
	
	hour =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][14];
	Tmep[1] =x.data[0][15];
	
	minute =atoi((char*)Tmep);
	
   TimeInit((2000 + year), month, day, hour, minute, second);
*/
	Tmep[0] =x.data[0][0];
	Tmep[1] =x.data[0][1];
	
	year =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][3];
	Tmep[1] =x.data[0][4];
	
	month =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][6];
	Tmep[1] =x.data[0][7];
	
	day =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][9];
	Tmep[1] =x.data[0][10];
	
	hour =atoi((char*)Tmep);
	
	Tmep[0] =x.data[0][12];
	Tmep[1] =x.data[0][13];
	
	minute =atoi((char*)Tmep);
	
	TimeInit((2000 + year), month, day, hour, minute, second);
	printf("ok\n");
  return 0;
}

/**
 * @function: msg_set_idn_fun
 * @brief:保存序列号和产品名称
 * @param {*}
 * @return {*}
 * @author: lzc
 */
uint8_t msg_set_idn_fun(Cmd_Info_Type x)
{
u8 err;
	if(x.data[0][0] =='P')
	{
	OSMutexPend(MutexIIC,200,&err);	
	  AT24CXX_Write(E2PROM_ADRESS, DATA_PRODUCT_NAME_ADDRESS,&(x.data[0][2]), 20);
	OSMutexPost(MutexIIC);
		printf("ok\n");
	}
	else if(x.data[0][0] =='S')
	{
	OSMutexPend(MutexIIC,200,&err);			
	  AT24CXX_Write(E2PROM_ADRESS, DATA_SN_ADDRESS, &(x.data[0][2]), 20);
	OSMutexPost(MutexIIC);		
		printf("ok\n");
	}
	else
	{
		printf("err_par\n");
	}
	 return 0;
}

unsigned char msg_set_threshold_name_fun(Cmd_Info_Type x)
{
	uint8 num = 0;
	u8 err;
	
	  Ost_Sup1();
	num =atoi((char*)x.data[0]);        //前两位是20
	num = num-1;
OSMutexPend(MutexIIC,200,&err);		
	WriteStringE2prom(x.data[1], PWF_NAME_ADDRESS + (num)*NAME_LENGTH);
OSMutexPost(MutexIIC);
	printf("ok\n");
	 Ost_Rem1();
 	return 0;
}

unsigned char msg_set_threshold_1310_fun(Cmd_Info_Type x)
{
	uint8 num = 0;
	uint8 cmd_num=0,cmd_len=0,position=0,i=0;
	uint8 a[3][6]= {{0},{0},{0}};
	fp32 bufpwf[3] ={0};
	u8 err;
	
  Ost_Sup1();
	num =atoi((char*)x.data[0]);        //前两位是20
	num = num-1;
	while (position < 30)
	{
		a[cmd_num][cmd_len] = x.data[1][position];
		if (x.data[1][position+1] ==',')
		{
				cmd_num ++;
				cmd_len =0;
				position=position+2;
		}
		else if(x.data[1][position+1] =='\n')
		{
			break;
		}
		else
		{
				cmd_len ++;
				position++;
		}
	}
	
	for(i = 0; i < 3; i++)
	{
		  bufpwf[i] = (float)atof((char*)a[i]);  
		//转浮点数 // 		//都是12个数据
		OSMutexPend(MutexIIC,200,&err);
			WriteFloatE2prom(bufpwf[i], PWF_ADDRESS + i * 4 + 12*0+(num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);
	}
	Ost_Rem1();
	printf("ok\n");
		return 0;
}

unsigned char msg_set_threshold_1490_fun(Cmd_Info_Type x)
{
	uint8 num = 0;
	uint8 cmd_num =0,cmd_len =0,position =0,i =0;
	uint8 a[3][6]= {{0},{0},{0}};
	fp32 bufpwf[3] ={0};
	u8 err;
	
	  Ost_Sup1();
	num =atoi((char*)x.data[0]);        //前两位是20
	num = num-1;
	while (position < 30)
	{
		a[cmd_num][cmd_len] = x.data[1][position];
		if (x.data[1][position+1] ==',')
		{
				cmd_num ++;
				cmd_len =0;
				position=position+2;
		}
		else if(x.data[1][position+1] =='\n')
		{
			break;
		}
		else
		{
				cmd_len ++;
				position++;
		}
	}
	
	for(i = 0; i < 3; i++)
	{
		  bufpwf[i] = (float)atof((char*)a[i]);  
		//转浮点数 都是12个数据
	 OSMutexPend(MutexIIC,200,&err);
			WriteFloatE2prom(bufpwf[i], PWF_ADDRESS + i * 4 +12*1+ (num) * 36); /*write 10 team of the PWF*/
	 OSMutexPost(MutexIIC);
	}
	printf("ok\n");
		Ost_Rem1();
		return 0;
}

unsigned char msg_set_threshold_1550_fun(Cmd_Info_Type x)
{
	uint8 num = 0;
	uint8 cmd_num =0,cmd_len =0,position =0,i =0;
	uint8 a[3][6]= {{0},{0},{0}};
	fp32 bufpwf[3] ={0};
		u8 err;
	
	 Ost_Sup1();
	num =atoi((char*)x.data[0]);        //前两位是20
	num = num-1;
	while (position < 30)
	{
		a[cmd_num][cmd_len] = x.data[1][position];
		if (x.data[1][position+1] ==',')
		{
				cmd_num ++;
				cmd_len =0;
				position=position+2;
		}
		else if(x.data[1][position+1] =='\n')
		{
			break;
		}
		else
		{
				cmd_len ++;
				position++;
		}
	}

	for(i = 0; i < 3; i++)
	{
		  bufpwf[i] = (float)atof((char*)a[i]);  
    OSMutexPend(MutexIIC,200,&err);                                              //都是12个数据
			WriteFloatE2prom(bufpwf[i], PWF_ADDRESS + i * 4 +12*2+ (num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);
	}
	printf("ok\n");
			Ost_Rem1();
		return 0;
}
#if FHP3P05
unsigned char msg_set_threshold_1270_fun(Cmd_Info_Type x)
{
	uint8 num = 0;
	uint8 cmd_num=0,cmd_len=0,position=0,i=0;
	uint8 a[3][6]= {{0},{0},{0}};
	fp32 bufpwf[3] ={0};
	u8 err;
	
	 Ost_Sup1();
	num =atoi((char*)x.data[0]);        //前两位是20
	num = num-1;
	while (position < 30)
	{
		a[cmd_num][cmd_len] = x.data[1][position];
		if (x.data[1][position+1] ==',')
		{
				cmd_num ++;
				cmd_len =0;
				position=position+2;
		}
		else if(x.data[1][position+1] =='\n')
		{
			break;
		}
		else
		{
				cmd_len ++;
				position++;
		}
	}
	
	for(i = 0; i < 3; i++)
	{
		  bufpwf[i] = (float)atof((char*)a[i]);  
		//转浮点数 //                               //都是12个数据
		 OSMutexPend(MutexIIC,200,&err); 
			WriteFloatE2prom(bufpwf[i], P05_PWF_ADDRESS + i * 4 + 12*0+(num) * 36); /*write 10 team of the PWF*/
		 OSMutexPost(MutexIIC);
	}
	printf("ok\n");
		Ost_Rem1();
		return 0;
}

unsigned char msg_set_threshold_1577_fun(Cmd_Info_Type x)
{
	uint8 num = 0;
	uint8 cmd_num =0,cmd_len =0,position =0,i =0;
	uint8 a[3][6]= {{0},{0},{0}};
	fp32 bufpwf[3] ={0};
	u8 err;
	
	Ost_Sup1();
	num =atoi((char*)x.data[0]);        //前两位是20
	num = num-1;
	while (position < 30)
	{
		a[cmd_num][cmd_len] = x.data[1][position];
		if (x.data[1][position+1] ==',')
		{
				cmd_num ++;
				cmd_len =0;
				position=position+2;
		}
		else if(x.data[1][position+1] =='\n')
		{
			break;
		}
		else
		{
				cmd_len ++;
				position++;
		}
	}

	for(i = 0; i < 3; i++)
	{
		  bufpwf[i] = (float)atof((char*)a[i]);  
    OSMutexPend(MutexIIC,200,&err);                                            //都是12个数据
			WriteFloatE2prom(bufpwf[i], P05_PWF_ADDRESS + i * 4 +12*2+ (num) * 36); /*write 10 team of the PWF*/
		OSMutexPost(MutexIIC);
	}
	printf("ok\n");
		Ost_Rem1();
		return 0;
}
#endif


//unsigned char msg_set_cal_reset_fun(Cmd_Info_Type x)
//{
//		uint8 i = 0;
//	  fp32 temp = 0;
////	fp32 bufpwf[9] = {0};
//	
//	
////	
////	  for(i = 0; i < 3; i++)
////    {
////        bufpwf[i] = ReadFloatE2prom(OFFSET_COPY_ADDRESS + i * 4); ////复位修正值
////    }
////#if FHP3P02
////    bufpwf[3] = ReadFloatE2prom(CAL_COPY_1310_C_ADDRESS); ////读取1310连续的修正值
////#endif
// 
//    for(i = 0; i < 3; i++)
//    {
//        temp = ReadFloatE2prom(OFFSET_COPY_ADDRESS + i * 4);
//        WriteFloatE2prom(temp, WAVE_OFFSET_ADDRESS + i * 4);
//    }
//#if FHP3P02
//    temp = ReadFloatE2prom(CAL_COPY_1310_C_ADDRESS); ////读取1310连续的修正值
//    WriteFloatE2prom(temp, CAL_1310_C_ADDRESS);
//#endif
//		
//#if FHP3P05
//		for(i = 0; i < 3; i++)
//    {
//        temp = ReadFloatE2prom(P05_OFFSET_COPY_ADDRESS + i * 4);
//        WriteFloatE2prom(temp, P05_WAVE_OFFSET_ADDRESS + i * 4);
//    }
//	#if FHP3P02
//    temp = ReadFloatE2prom(P05_CAL_COPY_1270_C_ADDRESS); ////读取1310连续的修正值
//    WriteFloatE2prom(temp, P05_CAL_1270_C_ADDRESS);
//	#endif
//#endif
//		
// 	printf("ok\n");
//	return 0;
//}

unsigned char msg_set_history_reset_fun(Cmd_Info_Type x)
{
    uint16 i = 0;
    uint8  j = 0;

    //OSTaskSuspend(TIME_PRIO);
    OSTaskSuspend(AUTOMATISM_PRIO);
    OSTaskSuspend(HOST_PRIO);
    OSTaskSuspend(POWEROFF_PRIO);
    OSTaskSuspend(SOURCE_PRIO);
//	   	PwmInitialize();
    FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
    Dis_String(100, 180, 16, 16, "PC Writing");
    for(i = 0; i < 997; i++)
    {
        WriteDataE2promTwo(DATA_SAVE_SP + i * 20, DATA_ADDRESS_SP + i * 2); ////初始化地址指针指向的数据地址
    }

    for(i = 3; i > 0; i--)
    {
        WriteDataE2promTwo(DATA_SAVE_SP + (1000 - i) * 20, DATA_ADDRESS_EMP_SP + (i - 1) * 2); ////初始化地址指空针指向的数据地址
    }
    WriteDataE2promTwo(997, DATA_SAVE_NUM_ADDRESS); ////初始化保存的数据个数为1000个。
    WriteDataE2promTwo(3, DATA_EMP_NUM_ADDRESS); ////初始化空指针的数据个数为0个。
    WriteDataE2promTwo(0, DATA_LAST_SEE_ADDRESS);

    for(i = 0; i < 997; i++)
    {
        for(j = 0; j < 3; j++)
        {
            WriteFloatE2prom1(20.0 - 0.1 * i, DATA_SAVE_SP + i * 20 + j * 4); ///数据的存储地址
        }
        if(i %2) //dB
        {
            WriteDataE2promByte(0x02, DATA_SAVE_SP + i * 20 + 18);
            for(j = 0; j < 3; j++)
            {
                WriteIntE2prom1(5.0 - 0.04 * i, DATA_SAVE_SP + i * 20 + 12 + j * 2);
            }
        }
        else	  //dBm
        {
            WriteDataE2promByte(0x00, DATA_SAVE_SP + i * 20 + 18);

        }
    }		
//	    PwmInitialize();								           //  退出写画面
    FillDisplay1(0, 240, 320, 240, 0xffff);
    FillDisplay1(100, 130, 80, 24, LCD_WHITE);
    g_c_ImageFlag = 0x00;
    HostImage();
    OSTaskResume(SOURCE_PRIO);
    OSTaskResume(POWEROFF_PRIO);
    OSTaskResume(HOST_PRIO);
    OSTaskResume(AUTOMATISM_PRIO);
    //OSTaskResume(TIME_PRIO);
 	 printf("ok\n");
	 return 0;
}

//////////////////////////////////////////校准命令/////////////////////////////////////////////

/**
 * @function: msg_cal_start_fun
 * @brief: 开始校准
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_cal_start_fun(Cmd_Info_Type x)
{
	//if(CAL_State.Cal_state ==Close)
//	{
	u8 err;
		OSMutexPend(MutexIIC,200,&err);		
		
		Ost_Sup();
			OSMutexPost(MutexIIC);	
		CAL_State.Cal_state =Open;
		FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
		printf("ok\n");
//	}
//	else
//	{
//		printf("err_cal\n");
//	}
  return 0;
}

/**
 * @brief 设置校准波长
 * @param
 * @return
 */
//unsigned char msg_cal_wav_fun(Cmd_Info_Type x)
unsigned char msg_set_wavelength_fun(Cmd_Info_Type x)
{
//	unsigned char  a0a1
			unsigned char wavelength;
//	char  buf[15] ={0};
	if(CAL_State.Cal_state ==Open)
	{
		wavelength =wavelength_trma(x);
		
		if(CAL_State.Cal_wavelength ==wavelength)
		{
		
		}
		else
		{
		//	FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
		}
		
		if(wavelength ==CW1310)
		{
//			a0a1 = 1;
//			ARM_LOGIC0 = 1 ;				/*disable the pulse of 1310 */
//			Set1310Adg704(a0a1);			/*set the 1 beeline */
//			delay_ms(200);

//			FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
//			Dis_BigNum(10, 235, 0x0000, "1310.0");
//			sprintf(buf, "%c", 'A');
//			strcat(buf, "uto Cal");
//			Dis_Num(160, 220, 12, 16, buf);
			
			CAL_State.Cal_wavelength =CW1310;
		}
		else if(wavelength ==CW1490)
		{	
//			FillDisplay1(0, 239, 320, 240, 0xffff);	        //清除画面
//			Dis_BigNum(10, 235, 0x0000, "1490.0");
//			sprintf(buf, "%c", 'A');
//			strcat(buf, "uto Cal");
//			Dis_Num(160, 220, 12, 16, buf);
			CAL_State.Cal_wavelength =CW1490;
		}
		else if(wavelength ==CW1550)
		{
//			FillDisplay1(0, 240, 320, 240, 0xffff);
//			Dis_BigNum(10, 235, 0x0000, "1550.0");
//			sprintf(buf, "%c", 'A');
//			strcat(buf, "uto Cal");
//			Dis_Num(160, 220, 12, 16, buf);
			
			CAL_State.Cal_wavelength =CW1550;
		}
		
		else if(wavelength ==BU1310)
		{
//			a0a1 = 1;
//			ARM_LOGIC0 = 0 ;				/*enable the pulse of 1310 */
//			Set1310Adg704(a0a1);			/*set the 1 beeline */
//			delay_ms(200);

//			FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
//			Dis_BigNum(10, 235, 0x0000, "1310.0");
//			sprintf(buf, "%c", 'A');
//			strcat(buf, "uto Cal");
//			Dis_Num(160, 220, 12, 16, buf);
			
			CAL_State.Cal_wavelength =BU1310;
		}
#if FHP3P05
		else if(wavelength ==BU1270)
		{
			CAL_State.Cal_wavelength =BU1270;
		}
		else if(wavelength ==CW1270)
		{
			CAL_State.Cal_wavelength =CW1270;
		}	
		else if(wavelength ==CW1577)
		{
			CAL_State.Cal_wavelength =CW1577;
		}	
#endif
		else
		{
			CAL_State.Cal_state =Close;
			printf("err_par\n");
			return 0;
		}
		printf("ok\n");
		return 0;
	}
	CAL_State.Cal_state =Close;
	printf("err_cal\n");
	return 0;
}

/**
 * @function: msg_cal_level_fun
 * @brief: 档位
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_cal_level_fun(Cmd_Info_Type x)
{
	if(CAL_State.Cal_state ==Open)
	{
		if(atoi((char*)x.data[0]) ==Cal_lev1)
		{
			CAL_State.Cal_level =Cal_lev1;
		}
		else if(atoi((char*)x.data[0]) ==Cal_lev2)
		{
			CAL_State.Cal_level =Cal_lev2;
		}
		else if(atoi((char*)x.data[0]) ==Cal_lev3)
		{
			CAL_State.Cal_level =Cal_lev3;
		}
		
		else if(atoi((char*)x.data[0]) ==Cal_lev4)
		{
			CAL_State.Cal_level =Cal_lev4;
		}
		else
		{
			CAL_State.Cal_state =Close;
			printf("err_par\n");
			return 0;
		}
		printf("ok\n");
		return 0;
	}
	CAL_State.Cal_state =Close;
	printf("err_cal\n");
	return 0;
}

/**
 * @brief 校准点,具有确定性
 * @param
 * @return
 */
uint8_t msg_cal_point_power_fun(Cmd_Info_Type x)
{
	float   	dbmcal,mwcal;
	uint8_t	   point;
	//unsigned char  wavelength;
	unsigned char  a0a1;
	unsigned char  i;
  char	         buf[15] = {0};
	static	float  voltage;
 // char	         buf_vol[15] = {0};
	
	if(CAL_State.Cal_wavelength ==CW1310)
	{
		a0a1 = 1;
		ARM_LOGIC0 = 1 ;				/*disable the pulse of 1310 */
		Set1310Adg704(a0a1);			/*set the 1 beeline */
		delay_ms(200);

	//	FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
		Dis_BigNum(10, 235, 0x0000, "1310.0");
		sprintf(buf, "%c", 'A');
		strcat(buf, "uto Cal");
	//	Dis_Num(160, 220, 12, 16, buf);
		Dis_String(160, 220, 12, 16, buf);
	}
	else if(CAL_State.Cal_wavelength ==CW1490)
	{	
	//	FillDisplay1(0, 239, 320, 240, 0xffff);	        //清除画面
		Dis_BigNum(10, 235, 0x0000, "1490.0");
		sprintf(buf, "%c", 'A');
		strcat(buf, "uto Cal");
		//Dis_Num(160, 220, 12, 16, buf);
		Dis_String(160, 220, 12, 16, buf);
		CAL_State.Cal_wavelength =CW1490;
	}
	else if(CAL_State.Cal_wavelength ==CW1550)
	{
	//	FillDisplay1(0, 240, 320, 240, 0xffff);
		Dis_BigNum(10, 235, 0x0000, "1550.0");
		sprintf(buf, "%c", 'A');
		strcat(buf, "uto Cal");
	//	Dis_Num(160, 220, 12, 16, buf);
			Dis_String(160, 220, 12, 16, buf);
	}

	else if(CAL_State.Cal_wavelength ==BU1310)
	{
		a0a1 = 1;
		ARM_LOGIC0 = 0 ;				/*enable the pulse of 1310 */
		Set1310Adg704(a0a1);			/*set the 1 beeline */
		delay_ms(200);

	//	FillDisplay1(0, 240, 320, 240, 0xffff);	        //清除画面
		Dis_BigNum(10, 235, 0x0000, "1310.0");
		sprintf(buf, "%c", 'A');
		strcat(buf, "uto Cal");
	//	Dis_Num(160, 220, 12, 16, buf);
		Dis_String(160, 220, 12, 16, buf);
	}

	point=atoi((char*)x.data[0]);                 //获取点
	dbmcal =(float)atof((char*)x.data[1]);        //获取功率 单位DBm
	CAL_State.point =point;                       
	i =(CAL_State.Cal_level)*2+CAL_State.point;   //液晶显示第几列
	
	if(CAL_State.Cal_state ==Open)
	{
		if(point==1 ||point==2)
		{
			if((-50>dbmcal)||(dbmcal>10))
			{
				CAL_State.Cal_state =Close;
				printf("err_par\n");
				return 0;
			}
			mwcal = pow(10.0, dbmcal / 10.0); //dbm转mw
			CAL_State.Cal_mw[(CAL_State.Cal_level)*2+point-1]=mwcal;
		}
		else
		{
			CAL_State.Cal_state =Close;
			printf("err_par\n");
			return 0;
		}
		
		sprintf(buf, "%d", i);				         //显示校准点序号
		strcat(buf, ":");
		Dis_Num(10, 180 - 20 * (i - 1), 12, 16, buf);
		// 显示标准的校准点
		if(SPRINT_ERROR)
		{
				MySprintf(buf, dbmcal);
		}
		else
		{
				sprintf(buf, " %3.1f", dbmcal);
		}
		strcat(buf, " dBm");
		Dis_Num(50, 180 - 20 * (i - 1), 12, 16, buf);
		
	
		a0a1 = CAL_State.Cal_level;	
		if(CAL_State.Cal_wavelength ==BU1310 ||CAL_State.Cal_wavelength ==CW1310)
		{
			if(CAL_State.Cal_wavelength ==BU1310)   //突发光
			{
				ARM_LOGIC0 = 0;							/*able the pulse of 1310 */
			}
			else
			{
				ARM_LOGIC0 = 1;							/*disable the pulse of 1310 */
			}
			
			Channel(AD7799_1310);     //AD口子确认
			delay_ms(50);
			Set1310Adg704(a0a1);
			ClrVolt1310();
			delay_ms(2);
			EnVolt1310();
		}
		
		else if(CAL_State.Cal_wavelength ==CW1490)
		{
			Channel(AD7799_1490);     //AD口子确认
			delay_ms(50);
			Set1490Adg704(a0a1);
			ReleaseVolt1490();
		}
		
		else if(CAL_State.Cal_wavelength ==CW1550)
		{
			Channel(AD7799_1550);     //AD口子确认
			delay_ms(50);   
			Set1550Adg704(a0a1);
			ReleaseVolt1550();
		}
		else
		{	
			printf("err_par\n");
			return 0;
		}

		delay_ms(2000);
		voltage = Ad7799Result();
		
		CAL_State.Cal_vol[(CAL_State.Cal_level)*2+CAL_State.point-1] = voltage;
		
		if(SPRINT_ERROR)										 // 显示采样电压值
		{
				FloatToStr(buf, 3, voltage);
				Dis_Num(190, 180 - 20 * (i - 1), 12, 16, buf);
		}
		else
		{
				sprintf(buf, "%5.4f\n", voltage);
				Dis_Num(190, 180 - 20 * (i - 1), 12, 16, buf);
		}
	//		printf("%f\n",voltage);
	//		return 0;
				
		printf("ok\n");
		return 0;
	}
	else
	{
		CAL_State.Cal_state =Close;
		printf("err_cal\n");
    return 0;
	}
}

/**
 * @function: msg_cal_save_fun
 * @brief: 保存校准值
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_cal_save_fun(Cmd_Info_Type x)
{
	uint8_t i;
	float 	bb1, bb2;
	//u8 err;
	//static float g_c_1310_c_rn[4],g_c_1310_c_rd[4],g_c_1490_rn[4],g_c_1490_rd[4],g_c_1550_rn[4],g_c_1550_rd[4];           //临时使用的变量！！！！！
	
	if(CAL_State.Cal_wavelength ==CW1310)
	{
		for(i = 0; i < 4; i++)						 /*compute the result of the k,b of beeline 0,1,2,3*/
		{
				bb1 = (CAL_State.Cal_vol[2 * i] - CAL_State.Cal_vol[2 * i + 1]);
				bb2 = (CAL_State.Cal_mw[2 * i] - CAL_State.Cal_mw[2 * i + 1]);
				g_c_1310_c_rn[i] = bb1 / bb2;
				g_c_1310_c_rd[i] = CAL_State.Cal_vol[2 * i + 1] - CAL_State.Cal_mw[2 * i + 1] * g_c_1310_c_rn[i];
		}

		for(i = 0; i < 4; i++)					 /*save the parameter slope rn,intercept rd of the beeline */
		{
			 g_c_1310_c_rn[i] =g_c_1310_c_rn[i];
	//	OSMutexPend(MutexIIC,200,&err);	
			 WriteFloatE2prom(g_c_1310_c_rn[i], RN_1310_C_ADRESS + i * 0x04);    //保存斜率
	//	OSMutexPost(MutexIIC);		
		}
		for(i = 0; i < 4; i++)
		{
			g_c_1310_c_rd[i] =g_c_1310_c_rd[i];
	//		OSMutexPend(MutexIIC,200,&err);	
				WriteFloatE2prom(g_c_1310_c_rd[i], RD_1310_C_ADRESS + i * 0x04);     //保存常数
	//		OSMutexPost(MutexIIC);	
		}
	//OSMutexPend(MutexIIC,200,&err);		
		
		WriteFloatE2prom(0.0, CAL_1310_C_ADDRESS);         //保存修正值
		WriteFloatE2prom(0.0, CAL_COPY_1310_C_ADDRESS);    //保存修正值备份
	//OSMutexPost(MutexIIC);	
		InitRnRd1310();                                    //更新校准参数
	}
	
	else if(CAL_State.Cal_wavelength ==CW1490)
	{
		for(i = 0; i < 4; i++)						 /*compute the result of the k,b of beeline 0,1,2,3*/
		{
				bb1 = (CAL_State.Cal_vol[2 * i] - CAL_State.Cal_vol[2 * i + 1]);
				bb2 = (CAL_State.Cal_mw[2 * i] - CAL_State.Cal_mw[2 * i + 1]);
				g_c_1490_rn[i] = bb1 / bb2;
				g_c_1490_rd[i] = CAL_State.Cal_vol[2 * i + 1] - CAL_State.Cal_mw[2 * i + 1] * g_c_1490_rn[i];
		}

		for(i = 0; i < 4; i++)					 /*save the parameter slope rn,intercept rd of the beeline */
		{
			 g_c_1490_rn[i] =g_c_1490_rn[i];
	//	OSMutexPend(MutexIIC,200,&err);		
				WriteFloatE2prom(g_c_1490_rn[i], RN_1490_ADRESS + i * 0x04);    //保存斜率
	//	OSMutexPost(MutexIIC);	
		}
		for(i = 0; i < 4; i++)
		{
			g_c_1490_rd[i] =g_c_1490_rd[i];
	//	OSMutexPend(MutexIIC,200,&err);	
				WriteFloatE2prom(g_c_1490_rd[i], RD_1490_ADRESS + i * 0x04);     //保存常数
	//	OSMutexPost(MutexIIC);	
		}
	//OSMutexPend(MutexIIC,200,&err);	
		WriteFloatE2prom(0.0, CAL_1490_ADDRESS);         //保存修正值
		WriteFloatE2prom(0.0, CAL_COPY_1490_ADDRESS);    //保存修正值备份
	//OSMutexPost(MutexIIC);	
		InitRnRd();                                      //更新校准参数
	}
	
	else if(CAL_State.Cal_wavelength ==CW1550)
	{
		for(i = 0; i < 4; i++)						 /*compute the result of the k,b of beeline 0,1,2,3*/
		{
				bb1 = (CAL_State.Cal_vol[2 * i] - CAL_State.Cal_vol[2 * i + 1]);
				bb2 = (CAL_State.Cal_mw[2 * i] - CAL_State.Cal_mw[2 * i + 1]);
				g_c_1550_rn[i] = bb1 / bb2;
				g_c_1550_rd[i] = CAL_State.Cal_vol[2 * i + 1] - CAL_State.Cal_mw[2 * i + 1] * g_c_1550_rn[i];
		}

		for(i = 0; i < 4; i++)					 /*save the parameter slope rn,intercept rd of the beeline */
		{
			 g_c_1550_rn[i] =g_c_1550_rn[i];
	//		OSMutexPend(MutexIIC,200,&err);		
				WriteFloatE2prom(g_c_1550_rn[i], RN_1550_ADRESS + i * 0x04);    //保存斜率
	//		OSMutexPost(MutexIIC);
		}
		for(i = 0; i < 4; i++)
		{
			g_c_1550_rd[i] =g_c_1550_rd[i];
	//	OSMutexPend(MutexIIC,200,&err);		
			WriteFloatE2prom(g_c_1550_rd[i], RD_1550_ADRESS + i * 0x04);     //保存常数
	//	OSMutexPost(MutexIIC);	
		}
//OSMutexPend(MutexIIC,200,&err);			
		WriteFloatE2prom(0.0, CAL_1550_ADDRESS);         //保存修正值
		WriteFloatE2prom(0.0, CAL_COPY_1550_ADDRESS);    //保存修正值备份
//OSMutexPost(MutexIIC);	
		InitRnRd();                                    //更新校准参数
	}
		printf("ok\n");
    return 0;
}

/**
 * @function: msg_cal_stop_fun
 * @brief: 停止校准
 * @param {Cmd_Info_Type} x
 * @return {*}
 * @author: lzc
 */
uint8_t msg_cal_stop_fun(Cmd_Info_Type x)
{
	if(CAL_State.Cal_state ==Open)
	{
		Ost_Rem();
		
		Init_CAL_State();
		printf("ok\n");
	}
	else
	{
		printf("err_par\n");
	}
    return 0;
}


unsigned char msg_cal_iso_1490_1550_fun(Cmd_Info_Type x)
{
	//u8 err;
	if(strcmp("1490_1550\n", (const char *)x.data[0]) == 0)
	{	
		g_c_1490_voltage = Voltage1490AotumatismTest();//测量1490nm 光强并计算光功率
		g_c_1490_pmw 	 = (g_c_1490_voltage - g_c_1490_rd[g_c_1490_a0a1]) / g_c_1490_rn[g_c_1490_a0a1];

		g_c_1550_voltage = Voltage1550AotumatismTest();//测量1550nm 光强并计算光功率	
		g_c_1550_pmw	 = (g_c_1550_voltage - g_c_1550_rd[g_c_1550_a0a1]) / g_c_1550_rn[g_c_1550_a0a1];
	
		Iso1490_1550 = g_c_1490_pmw / g_c_1550_pmw;
	//OSMutexPend(MutexIIC,200,&err);	
		WriteFloatE2prom(Iso1490_1550, ISO_1490_1550_ADDRESS);//计算并保存1490nm对1550nm隔离度
	//OSMutexPost(MutexIIC);	
		printf("ok\n");
	}
	else if(strcmp("1550_1490\n", (const char *)x.data[0]) == 0)
	{
		g_c_1490_voltage = Voltage1490AotumatismTest(); //测量1490nm 光强并计算光功率
		g_c_1490_pmw 	 = (g_c_1490_voltage - g_c_1490_rd[g_c_1490_a0a1]) / g_c_1490_rn[g_c_1490_a0a1];

		g_c_1550_voltage = Voltage1550AotumatismTest(); //测量1550nm 光强并计算光功率	
		g_c_1550_pmw	 = (g_c_1550_voltage - g_c_1550_rd[g_c_1550_a0a1]) / g_c_1550_rn[g_c_1550_a0a1];
	
		Iso1550_1490 = g_c_1550_pmw / g_c_1490_pmw;
	//OSMutexPend(MutexIIC,200,&err);		
		WriteFloatE2prom(Iso1550_1490, ISO_1550_1490_ADDRESS);//计算并保存1550nm对1490nm隔离度
//OSMutexPost(MutexIIC);		
		printf("ok\n");
	}
	else
	{	
		printf("err_par\n");
	}

	return 0;
}
 
 
//unsigned char msg_cal_iso_1550_1490_fun(Cmd_Info_Type x)
//{
//	g_c_1490_voltage = Voltage1490AotumatismTest(); //测量1490nm 光强并计算光功率
//	g_c_1490_pmw 	 = (g_c_1490_voltage - g_c_1490_rd[g_c_1490_a0a1]) / g_c_1490_rn[g_c_1490_a0a1];

//	g_c_1550_voltage = Voltage1550AotumatismTest(); //测量1550nm 光强并计算光功率
//	g_c_1550_pmw	 = (g_c_1550_voltage - g_c_1550_rd[g_c_1550_a0a1]) / g_c_1550_rn[g_c_1550_a0a1];
//	
//	Iso1550_1490 = g_c_1550_pmw / g_c_1490_pmw;
//	WriteFloatE2prom(Iso1550_1490, ISO_1550_1490_ADDRESS);//计算并保存1550nm对1490nm隔离度
//	 printf("ok\n");
//	return 0;
//}


unsigned char msg_set_cal_reset_fun(Cmd_Info_Type x)
{
	uint8 i = 0;
	fp32 temp = 0;
	u8 err;
//	fp32 bufpwf[9] = {0};
	
//	
//	  for(i = 0; i < 3; i++)
//    {
//        bufpwf[i] = ReadFloatE2prom(OFFSET_COPY_ADDRESS + i * 4); ////复位修正值
//    }
//#if FHP3P02
//    bufpwf[3] = ReadFloatE2prom(CAL_COPY_1310_C_ADDRESS); ////读取1310连续的修正值
//#endif
 	OSMutexPend(MutexIIC,200,&err);
    for(i = 0; i < 3; i++)
    {
       temp = ReadFloatE2prom(OFFSET_COPY_ADDRESS + i * 4);  //是加法	
       WriteFloatE2prom(temp, WAVE_OFFSET_ADDRESS + i * 4);
    }
	
#if FHP3P02
		
    temp = ReadFloatE2prom(CAL_COPY_1310_C_ADDRESS); ////读取1310连续的修正值
    WriteFloatE2prom(temp, CAL_1310_C_ADDRESS);

#endif
#if FHP3P05
		for(i = 0; i < 3; i++)
		{
			temp = ReadFloatE2prom(P05_OFFSET_COPY_ADDRESS + i * 4);  //是加法
			WriteFloatE2prom(temp, P05_WAVE_OFFSET_ADDRESS + i * 4);
		}
	#if FHP3P02
    temp = ReadFloatE2prom(P05_CAL_COPY_1270_C_ADDRESS); ////读取1310连续的修正值
    WriteFloatE2prom(temp, P05_CAL_1270_C_ADDRESS);
	#endif	
#endif		

	
	 g_c_1310_c_cal = ReadFloatE2prom(CAL_1310_C_ADDRESS);
#if FHP3P02
    g_c_1310_p_cal = ReadFloatE2prom(CAL_1310_P_ADDRESS);
#else
    g_c_1310_p_cal = g_c_1310_c_cal;
#endif
    g_c_1490_cal = ReadFloatE2prom(CAL_1490_ADDRESS);
    g_c_1550_cal = ReadFloatE2prom(CAL_1550_ADDRESS);
		
		g_l_1310_c_copy_cal = g_c_1310_c_cal;	  //与修正值菜单显示有关
		g_l_1310_p_copy_cal = g_c_1310_p_cal;
		g_l_1490_copy_cal = g_c_1490_cal;
		g_l_1550_copy_cal = g_c_1550_cal;			
		
		
#if FHP3P05
	  g_c_1270_c_cal = ReadFloatE2prom(P05_CAL_1270_C_ADDRESS);
 #if FHP3P02
    g_c_1270_p_cal = ReadFloatE2prom(P05_CAL_1270_P_ADDRESS);
 #else
    g_c_1270_p_cal = g_c_1270_c_cal;
 #endif
	  g_c_1577_cal = ReadFloatE2prom(P05_CAL_1577_ADDRESS);		
		
		g_l_1270_c_copy_cal = g_c_1270_c_cal;	  //与修正值菜单显示有关
		g_l_1270_p_copy_cal = g_c_1270_p_cal;
		g_l_1577_copy_cal = g_c_1577_cal;		
		
#endif	
 OSMutexPost(MutexIIC);	
 	printf("ok\n");
	return 0;
}

unsigned char msg_cal_wave_offset_fun(Cmd_Info_Type x)
{
	unsigned char wavelength;
//	unsigned  char position = 0;
 // unsigned  char cmd_len = 0;
//	char a[10]={0};
	fp32 bufpwf[1] = {0};
	u8 err;
	
	//wavelength =wavelength_trma(x);
	
	if(strcmp("CW1310", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1310;
	}
	else if(strcmp("CW1490", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1490;
	}
	else if(strcmp("CW1550", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1550;
	}
	else if(strcmp("BU1310", (const char *)x.data[0]) == 0)
	{
		wavelength =BU1310;
	}
#if FHP3P05
	else if(strcmp("CW1270", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1270;
	}
	else if(strcmp("CW1577", (const char *)x.data[0]) == 0)
	{
		wavelength =CW1577;
	}
	else if(strcmp("BU1270", (const char *)x.data[0]) == 0)
	{
		wavelength =BU1270;
	}
#endif	
	else
	{
		wavelength  =0;
	}
	
	 bufpwf[0] =(float)atof((char*)x.data[1]);                    //转浮点数
OSMutexPend(MutexIIC,200,&err);	
   if(wavelength==BU1310)
	 {
        WriteFloatE2prom(bufpwf[0], WAVE_OFFSET_ADDRESS );    //修改修正值 
      g_c_1310_p_cal=bufpwf[0];
	 }
	 else if(wavelength==CW1490)
	 {
        WriteFloatE2prom(bufpwf[0], WAVE_OFFSET_ADDRESS + 4); //修改修正值
		  g_c_1490_cal=bufpwf[0];
	 }
	 else if(wavelength==CW1550)
	 {
			WriteFloatE2prom(bufpwf[0], WAVE_OFFSET_ADDRESS + 8);  
		   g_c_1550_cal=bufpwf[0];
	 }
	 else if(wavelength==CW1310)
	 {
      WriteFloatE2prom(bufpwf[0], CAL_1310_C_ADDRESS);  ////FHP3P02中修改1310连续的修正值
		   g_c_1310_c_cal=bufpwf[0];
	 }
	 
#if FHP3P05
	 else if(wavelength==CW1270)
	 {
      WriteFloatE2prom(bufpwf[0],P05_CAL_1270_C_ADDRESS); //修改修正
		  g_c_1270_c_cal =bufpwf[0];
	 }
	 else if(wavelength==CW1577)
	 {
			WriteFloatE2prom(bufpwf[0],P05_WAVE_OFFSET_ADDRESS + 8);  
		 g_c_1577_cal =bufpwf[0];
	 }
	 else if(wavelength==BU1270)
	 {
      WriteFloatE2prom(bufpwf[0],P05_WAVE_OFFSET_ADDRESS);  ////FHP3P02中修改1310连续的修正值
		  g_c_1270_p_cal =bufpwf[0];
	 }
	 g_l_1270_c_copy_cal = g_c_1270_c_cal;	  //与修正值菜单显示有关
	 g_l_1270_p_copy_cal = g_c_1270_p_cal;
	 g_l_1577_copy_cal = g_c_1577_cal;	
	 
#endif
OSMutexPost(MutexIIC); 
	 g_l_1310_c_copy_cal = g_c_1310_c_cal;	  //与修正值菜单显示有关
	 g_l_1310_p_copy_cal = g_c_1310_p_cal;
	 g_l_1490_copy_cal = g_c_1490_cal;
	 g_l_1550_copy_cal = g_c_1550_cal;	 
						
 	printf("ok\n");
	return 0;
}

/*
 * @function: msg_sys_upgrade_fun
 * @brief:系统升级
 * @param {*}
 * @return {*}
 * @author: lzc
 */
uint8_t msg_sys_upgrade_fun(Cmd_Info_Type x)
{
		__disable_irq();    
    FLASH_Unlock();
	
		FLASH_ErasePage(0X0807F800);
	
    FLASH_ProgramHalfWord(0X0807F800, 0X4321);
	
	
    NVIC_SystemReset();
    return 0;
}



